package com.mail.controller;

import com.mail.common.DataResult;
import com.mail.dto.EmailTeamDto;
import com.mail.dto.TeamMemberDto;
import com.mail.service.EmailTeamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user/team")
public class EmailTeamController {
    @Autowired
    private EmailTeamService emailTeamService;

    /**
     * 修改团队相关信息
     * 可修改：teamName,teamManager,teamNotes
     */
    @PutMapping("/updateTeam")
    public DataResult updateTeam(@RequestParam Map<String,Object> map){
        String id = (String) map.get("id");
        if (id.length() == 0) {
            return DataResult.error("请输入id！");
        }
        return emailTeamService.updateTeam(map);
    }


    /**
     * 创建团队
     * 注册信息：teamName,teamManager,teamNotes
     */
    @PostMapping("/addTeam")
    public DataResult addTeam(EmailTeamDto emailTeamDto){
        return emailTeamService.addTeam(emailTeamDto);
    }

    /**
     * 分页查询自己参与的团队
     * 分页查询信息：page，limit，id
     */
    @GetMapping("/queryTeamPage")
    public DataResult queryTeamPage(@RequestParam Map<String,Object> map){
        String page = (String) map.get("page");
        String limit = (String) map.get("limit");
        String id=(String) map.get("id");
        if (page.length() == 0 || limit.length() == 0||id.length()==0) {
            return DataResult.error("请传入参数！");
        }
        if (Long.parseLong(page) < 0 || Long.parseLong(limit) < 0) {
            return DataResult.error("请传入正确的参数！");
        }
        return emailTeamService.queryTeamPage(map);
    }

    /**
     * 删除自己创建的团队
     * 删除依据：团队id
     */
    @DeleteMapping("/deleteTeamById/{id}")
    public DataResult deleteTeamById(@PathVariable long id){
        return emailTeamService.deleteTeamById(id);
    }

    /**
     * 删除团队中的某个成员
     * 删除依据：表id
     */
    @DeleteMapping("/deleteMemberById/{id}")
    public DataResult deleteMemberById(@PathVariable long id){
        return emailTeamService.deleteMemberById(id);
    }

    /**
     * 添加团队成员
     * 添加信息：teamId，memberId
     */
    @PostMapping("/addMember")
    public DataResult addMember(TeamMemberDto teamMemberDto){
        return emailTeamService.addMember(teamMemberDto);
    }

    /**
     * 分页查询某个团队下的成员
     * 分页查询信息：page，limit，id
     */
    @GetMapping("/queryMemberPage")
    public DataResult queryMemberPage(@RequestParam Map<String,Object> map){
        String page = (String) map.get("page");
        String limit = (String) map.get("limit");
        String id=(String) map.get("id");
        if (page.length() == 0 || limit.length() == 0||id.length()==0) {
            return DataResult.error("请传入参数！");
        }
        if (Long.parseLong(page) < 0 || Long.parseLong(limit) < 0) {
            return DataResult.error("请传入正确的参数！");
        }
        return emailTeamService.queryMemberPage(map);
    }

    /**
     * 根据id查询团队
     * 查询信息：id
     */
    @GetMapping("/queryTeamById/{id}")
    public DataResult queryTeamById (@PathVariable long id){
        return emailTeamService.queryTeamById(id);
    }
}
